/**登陆拦截器
 * @author David
 *
 */
package com.qlm.interceptor;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.qlm.view.AdminView;

public class AuthInterceptor implements com.jfinal.aop.Interceptor {
	protected final static Logger logger = LoggerFactory.getLogger(AuthInterceptor.class);
	
	public static String status_session_out = "session_out_qlm";//session超时返回前台状态

	@Override
	public void intercept(Invocation inv) {
		Controller controller = inv.getController();
		AdminView admin = controller.getSessionAttr("adminView");
		if (admin != null) {
			inv.invoke();
		} else {
			if(isAjaxReq(controller.getRequest())){
				controller.renderJson(status_session_out);
			}else{
				controller.redirect("/login");
			}
		}

	}

	private boolean isAjaxReq(HttpServletRequest req) {
		boolean ajax = "XMLHttpRequest".equals(req.getHeader("X-Requested-With"));
		return ajax;
	}

}